package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.TeachPlanSign;
import com.yqg.education.entity.TrainSign;
import com.yqg.education.mapper.TeachPlanMapper;
import com.yqg.education.mapper.TeachPlanSignMapper;
import com.yqg.education.service.ITeachPlanSignService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * TeachPlanSignController 前端控制器
 * @author zxf
 * @since 2025-05-12
 */
@RestController
@RequestMapping("/teach-plan-sign")
public class TeachPlanSignController {

    @Resource
    private ITeachPlanSignService teachPlanSignService;
    @Resource
    private TeachPlanSignMapper teachPlanSignMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody TeachPlanSign teachPlanSign) {
        return Result.success(teachPlanSignService.saveOrUpdate(teachPlanSign));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        TeachPlanSign teachPlanSign =teachPlanSignService.getById(id);
        return Result.success(teachPlanSignService.saveOrUpdate(teachPlanSign));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<TeachPlanSign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<TeachPlanSign> list=  teachPlanSignService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(teachPlanSignService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String username,
                           String courseName,
                           String teachPlanName,
                           @RequestParam(required = false) Integer flow,
                           @RequestParam(required = false) Integer stuId) {
        pageNum = pageNum * pageSize - pageSize;
        List<TeachPlanSign> teachPlanSignList = teachPlanSignMapper.findPage(pageNum, pageSize, username, courseName, teachPlanName, flow, stuId);
        Long count = teachPlanSignMapper.findCount(username, courseName, teachPlanName, flow, stuId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", teachPlanSignList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 修改学员端查看资源完成度flow
     */
    @GetMapping("/stuUpdateTeachPlanSignFlow")
    public Result stuUpdateTeachPlanSignFlow(@RequestParam Long teachPlanId,
                                         @RequestParam Long stuId){
        TeachPlanSign teachPlanSign = new TeachPlanSign();
        teachPlanSign.setStuId(stuId);
        teachPlanSign.setTeachPlanId(teachPlanId);
        teachPlanSign.setFlow(1);
        teachPlanSign.setCreateTime(LocalDateTime.now());
        teachPlanSignMapper.insert(teachPlanSign);
        return Result.success();
    }
}

